﻿using Newtonsoft.Json;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Web;


namespace Bricks.Component.Utils
{
    /// <summary>
    /// IPHelper
    /// </summary>
    public static class IpHelper
    {
        /// <summary> 
        /// 取得客户端真实IP地址
        /// </summary> 
        public static string IPAddress
        {
            get
            {
                string result = string.Empty;

                result = HttpContext.Current.Request.ServerVariables["HTTP_X_FORWARDED_FOR"];

                if (!string.IsNullOrEmpty(result))
                {
                    //可能有代理 
                    if (result.IndexOf(".") != -1) //没有“.”肯定是非IPv4格式 
                    {
                        if (result.IndexOf(",") != -1) { return result; }
                        else if (IsIPAddress(result)) { return result; }//代理即是IP格式 
                        else { result = null; }//代理中的内容 非IP，取IP 
                    }
                    else { result = null; }
                }

                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.ServerVariables["REMOTE_ADDR"];
                }
                if (string.IsNullOrEmpty(result))
                {
                    result = HttpContext.Current.Request.UserHostAddress;
                }
                return result;
            }
        }

        /// <summary> 
        /// 判断是否是IP地址格式 0.0.0.0 
        /// </summary> 
        /// <param name="ip">待判断的IP地址</param> 
        /// <returns>true or false</returns> 
        public static bool IsIPAddress(string ip)
        {
            if (string.IsNullOrEmpty(ip) || ip.Length < 7 || ip.Length > 15) { return false; }

            string regformat = @"^\d{1,3}[\.]\d{1,3}[\.]\d{1,3}[\.]\d{1,3}$";

            Regex regex = new Regex(regformat, RegexOptions.IgnoreCase);

            return regex.IsMatch(ip);
        }

        /// <summary>
        /// 获取IP所在区域
        /// </summary>
        /// <param name="ip">IP地址</param>
        /// <returns>结果</returns>
        public static IPCheckResult IPInfo(string ip)
        {
            if (!IsIPAddress(ip)) { return null; }

            string url = "http://ip.taobao.com/service/getIpInfo.php?ip=" + ip;

            string json = HttpHelper.GetPageHtml(url, null);

            return JsonConvert.DeserializeObject<IPCheckResult>(json);
        }
    }

    /// <summary>
    /// 基础数据
    /// </summary>
    public class IPCheckResult
    {
        public int code;
        public IPAdderssData data;
    }

    /// <summary>
    /// 信息内容
    /// </summary>
    public class IPAdderssData
    {
        public string country;
        public string country_id;
        public string area;
        public string area_id;
        public string region;
        public string region_id;
        public string city;
        public string city_id;
        public string county;
        public string county_id;
        public string isp;
        public string isp_id;
        public string ip;
    }
}